package com.ps.camera;

import java.io.File;
import java.io.IOException;

import org.apache.log4j.Logger;

import android.content.Context;
import android.hardware.Camera;
import android.os.Handler;
import android.os.Message;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
	private SurfaceHolder mHolder;
	private Camera mCamera;
	private Handler mHandler;
	
	Logger log = Logger.getLogger(CameraPreview.class);
	
	public CameraPreview(Context context, Camera camera) {
		super(context);
		
		File storagePath = SLogger.getStoragePath();
		SLogger.loadLogConfigurator(storagePath);
		log.info("CameraPreview Loaded");
		
		mCamera = camera;
		//Install a SurfaceHolder.Callback so we get notified when the underlying surface is created and destroyed.
        mHolder = getHolder();
        mHolder.addCallback(this);
        
        //deprecated setting, but required on Android versions prior to 3.0
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
	}
	
	public void surfaceCreated(SurfaceHolder holder) {
        //The surface has been created, now tell the camera where to draw the preview
        try {
            mCamera.setPreviewDisplay(holder);
        } catch (IOException e) {
        	log.error("Error setting camera preview" + e.getMessage());
        }
    }
	
	public void surfaceDestroyed(SurfaceHolder holder) {
        //Take care of releasisng the Camera preview in your activity.
    	releaseCamera();
    }
	
	private void releaseCamera() {
		if (mCamera != null) {
			mCamera.setPreviewCallback(null);
			// release the camera for other applications
			mCamera.release();
			mCamera = null;
		}
	}
	
	public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        //The Surface has been created, now tell the camera where to draw the preview
        if (mHolder.getSurface() == null) {
            //preview surface does not exist
            return;
        }

        //stop preview before making changes
        try {
            mCamera.stopPreview();
        } catch (Exception e) {
            //ignore: tried to stop a non-existent preview
        }

        //set preview size and make any resize, rotate or reformatiing changes here

        //start preview with new settings
        try {
            mCamera.setPreviewDisplay(mHolder);
            mCamera.startPreview();
            mCamera.setPreviewCallback(new Camera.PreviewCallback() {
            	public void onPreviewFrame(byte[] data, Camera camera) {
            		try {
            			
            			int mCameraHeight = camera.getParameters().getPreviewSize().height;
            			int mCameraWidth = camera.getParameters().getPreviewSize().width;
            			
            			Message m = new Message();
            			m.arg1 = mCameraWidth;
            			m.arg2 = mCameraHeight;
            			m.obj = data;
            			
            			if (mHandler != null)
            				mHandler.sendMessage(m);
            			
            		} catch(Exception e) {
            			if (e.getMessage() != null)
            				log.error(e.getMessage());
            			else
            				e.getStackTrace();
            		}
            	}
            });
        } catch (Exception e) {
            log.error("Error starting camera preview: " + e.getMessage());
        }
    }
	
	public void setHandler(Handler handler)
	{
		mHandler = handler;
	}
}
    
    
    
